﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;


namespace TortoisePlugin
{
    using TortoisePlugin.Settings;
    using System.IO;

    [ComVisible(true)]
    [ProgId("Tortoise Plugin")]
    [Guid("93b72f83-98a3-42f9-8392-9962c6833dec")]
    [ClassInterface(ClassInterfaceType.None)]
    public class Plugin : Interop.BugTraqProvider.IBugTraqProvider2, Interop.BugTraqProvider.IBugTraqProvider
    {
        protected Common.Forms.IBugSelection frmBS = null;
        protected Common.Forms.formDefaultSettings settings_forms_ = new Common.Forms.formDefaultSettings();
        private Settings.SettingsControl settings_control_ = new SettingsControl();
        private Settings.SettingsManager settings_ = new Settings.SettingsManager();
        protected DirectoryInfo project_directory_ = null;

        public Plugin()
        {
            settings_forms_.AddSettings(settings_, settings_control_);
            settings_control_.DefaultTortoiseClick += new EventHandler(settings_control__DefaultTortoiseClick);
        }

        void settings_control__DefaultTortoiseClick(object sender, EventArgs e)
        {
            settings_forms_.UpdateSettingsFromDialog();
            settings_.SaveSettings(false, this.GetType().FullName);
        }

        protected virtual void SetParameters(string parameters)
        {
            if (settings_.SettingsExists(false, this.GetType().FullName))
                settings_.LoadSettings(false, this.GetType().FullName, false);

            settings_.Set(parameters);
        }

        // Done
        public virtual bool ValidateParameters(IntPtr hParentWnd, string parameters)
        {
            SetParameters(parameters);

            return true;
        }

        // Done
        public virtual string GetLinkText(IntPtr hParentWnd, string parameters)
        {
            return "Choose Bug ID";
        }

        // Done
        public virtual string GetCommitMessage(IntPtr hParentWnd,
            string parameters,
            string commonRoot,
            string[] pathList,
            string originalMessage)
        {
            string[] revPropNames;
            string[] revPropValues;
            string bugOut;

            return GetCommitMessage2(hParentWnd, parameters, "", commonRoot, pathList, originalMessage, "", out bugOut, out revPropNames, out revPropValues);
        }

        // Done
        public virtual string GetCommitMessage2(IntPtr hParentWnd,
            string parameters,
            string commonURL,
            string commonRoot,
            string[] pathList,
            string originalMessage,
            string bugID,
            out string bugIDOut,
            out string[] revPropNames,
            out string[] revPropValues)
        {
            // If no revision properties are to be set, 
            // the plug-in MUST return empty arrays. 
            revPropNames = new string[0];
            revPropValues = new string[0];

            SetParameters(parameters);

            string new_message = originalMessage;

            try
            {
                new_message = new_message.Replace(frmBS.Message, "");

                if (frmBS.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    //Set selected bugid
                    bugIDOut = frmBS.BugId;

                    new_message += Environment.NewLine + frmBS.Message;

                    return new_message;
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.ToString());
                throw;
            }

            frmBS = null;
            bugIDOut = bugID;
            return originalMessage;
        }

        // Done
        public virtual string CheckCommit(IntPtr hParentWnd,
            string parameters,
            string commonURL,
            string commonRoot,
            string[] pathList,
            string commitMessage)
        {
            SetParameters(parameters);

            project_directory_ = new DirectoryInfo(commonURL);

            return "";
        }

        public virtual string OnCommitFinished(
            IntPtr hParentWnd,
            string commonRoot,
            string[] pathList,
            string logMessage,
            int revision)
        {
            if (frmBS != null)
            {
                string note = string.Empty;
                note += logMessage.Replace(frmBS.Message, "");

                if (!frmBS.AddCheckIn(project_directory_ == null ? string.Empty : project_directory_.Name, note, Revision(revision), pathList))
                    return "an error occured while connecting to server";
            }

            return "";
        }

        protected String Revision(int revision)
        {
            switch (settings_.eTortoise)
            {
                case TortoisePlugin.Settings.SettingsManager.eTORTOISE.eTORTOISE_GIT:
                    return ((uint)revision).ToString("X8") + "...";
                case TortoisePlugin.Settings.SettingsManager.eTORTOISE.eTORTOISE_SVN_CVS_HG:
                default:
                    return ((uint)revision).ToString();
            }
        }

        // Done
        public virtual bool HasOptions()
        {
            return true;
        }

        // Done
        public virtual string ShowOptionsDialog(IntPtr hParentWnd, string parameters)
        {
            SetParameters(parameters);

            if (settings_forms_.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                return settings_forms_.Options;
            }

            return parameters;
        }
    }
}
